/*
 * Copyright (c) 2016. zhaojin 15398699939@163.com
 */

package com.bankscene.screenfit.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.CheckResult;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bankscene.screenfit.handler.BackHandlerHelper;
import com.bankscene.screenfit.handler.FragmentBackHandler;
import com.bankscene.screenfit.vus.Vu;

import org.greenrobot.eventbus.EventBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public abstract class BasePresenterFragment<V extends Vu> extends Fragment implements FragmentBackHandler {

    private Logger log = LoggerFactory.getLogger(BasePresenterFragment.class);
    protected V vu;
    protected EventBus bus;
    private String simpleName;

    @Override//强调
    public final void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public final void onAttach(Context context) {
        super.onAttach(context);
        simpleName = getClass().getSimpleName();
        log.info("{} onAttach called... context: {}", simpleName, context);
    }

    @Override
    public final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        log.info("{} onCreate called... savedInstanceState: {}", simpleName, savedInstanceState);
        if (isUseEventBus()) {
            bus = EventBus.getDefault();
        }
        init();
    }

    /**
     * 子类实现以进行一些必要的初始化
     */
    protected void init() {}

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        log.info("{} onCreateView called...", simpleName);
        View view = null;
        try {
            vu = getVuClass().newInstance();
            vu.init(inflater, container, bus);
            onBindVu();
            view = vu.getRootView();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return view;
    }

    @Override
    public final void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        log.info("{} onViewCreated called...", simpleName);
    }

    @Override
    public final void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        log.info("{} onViewStateRestored called...", simpleName);
    }

    @Override
    public final void onStart() {
        super.onStart();
        log.info("{} onStart called...", simpleName);
    }

    @Override
    public final void onResume() {
        super.onResume();
        log.info("{} onResume called...", simpleName);
        afterResume();
    }

    protected void afterResume() {
        if (isUseEventBus()) {
            bus.register(this);
        }
    }

    @Override
    public final void onPause() {
        log.info("{} onPause called...", simpleName);
        beforePause();
        super.onPause();
    }

    protected void beforePause() {
        if (isUseEventBus()) {
            bus.unregister(this);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        log.info("{} onSaveInstanceState called... outState: {}", simpleName, outState);
        super.onSaveInstanceState(outState);
    }

    @Override
    public final void onStop() {
        log.info("{} onStop called...", simpleName);
        super.onStop();
    }

    @Override
    public final void onDestroyView() {
        log.info("{} onDestroyView called...", simpleName);
        onDestroyVu();
        vu.onUnwrap();
        vu = null;
        super.onDestroyView();
    }

    protected void onDestroyVu() {
    }

    @Override
    public final void onDestroy() {
        log.info("{} onDestroy called...", simpleName);
        super.onDestroy();
    }

    @Override
    public final void onDetach() {
        log.info("{} onDestroy called...", simpleName);
        super.onDetach();
    }

    protected void onBindVu() {
    }

    @CheckResult
    protected abstract Class<V> getVuClass();

    @CheckResult
    protected boolean isUseEventBus() {
        return false;//默认不使用Fragment 级别的EventBus,而是使用父容器Activity的EventBus,如子类需要使用,请复写该方法
    }

    @Override
    public boolean onBackPressed() {
        vu.onFragmentBackPressed();
        return BackHandlerHelper.handleBackPress(this);
    }
}